/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.xtandemparser.xmlreader.operations.indexer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author Kenneth
 */
public class RAFIndexer {

    private static ArrayList<Long> proteinOffsets = new ArrayList<>();
    private static ArrayList<Long> peptideOffsets = new ArrayList<>();
    private static ArrayList<Long> groupOffsets = new ArrayList<>();
    private static final Logger logger = Logger.getLogger(RAFIndexer.class);

    public static void makeIndices(File xmlFile) throws FileNotFoundException, IOException {
        Logger.getRootLogger().setLevel(Level.ALL);
        logger.info("Indexing XML file...");
        xmlFile = new File("C:\\Users\\Kenneth\\Documents\\NetBeansProjects\\xTandemParser\\src\\resources\\3643.t.xml");
        RandomAccessFile raf = new RandomAccessFile(xmlFile, "rw");
        String line = "";
        long startingLine = 0;
        while ((line = raf.readLine()) != null) {
            if (line.toUpperCase().contains("<PROTEIN")) {
                proteinOffsets.add(startingLine);
                logger.trace(line);
            } else if (line.toUpperCase().contains("<PEPTIDE")) {
                peptideOffsets.add(startingLine);
                logger.trace(line);
            } else if (line.toUpperCase().contains("<GROUP")) {
                peptideOffsets.add(startingLine);
                logger.trace(line);
            } else {
                startingLine = raf.getFilePointer();
            }
        }
        logger.debug(proteinOffsets.size() + " protein entries were found in the file");
        logger.debug(peptideOffsets.size() + " peptide entries were found in the file");
        logger.debug(groupOffsets.size() + " group entries were found in the file");
    }

    public static ArrayList<Long> getProteinIndices() {
        return proteinOffsets;
    }

    public static ArrayList<Long> getPeptideIndices() {
        return peptideOffsets;
    }

    public static ArrayList<Long> getGroupIndices() {
        return groupOffsets;
    }
}
